import tkinter as tk
from tkinter import ttk
from utils.database import get_db, get_users

class UserApp:
    def __init__(self, root):
        self.root = root
        self.root.title("User Information")

        self.tree = ttk.Treeview(root, columns=("user_id", "username", "role", "phone", "created_at"), show='headings')
        self.tree.heading("user_id", text="User ID")
        self.tree.heading("username", text="Username")
        self.tree.heading("role", text="Role")
        self.tree.heading("phone", text="Phone")
        self.tree.heading("created_at", text="Created At")
        self.tree.pack(fill=tk.BOTH, expand=True)

        self.load_users()

    def load_users(self):
        with get_db() as db:
            users = get_users(db)
            for user in users:
                self.tree.insert("", tk.END, values=(
                    user.user_id,
                    user.username,
                    user.role,
                    user.phone,
                    user.created_at.strftime("%Y-%m-%d %H:%M:%S")
                ))

if __name__ == "__main__":
    root = tk.Tk()
    app = UserApp(root)
    root.mainloop()